<template>
  <div>
    <Form :model="queryParams" :label-width="0" label-position="right">
      <FormItem style="margin-bottom: 10px">
        <Input search enter-button placeholder="搜索插件" v-model="queryParams.searchCode"
               @on-search="query(undefined,1)" style="width:250px"/>
      </FormItem>
    </Form>
    <Button @click="addSpPlugin" type="primary" style="margin-bottom:10px">
      <Icon type="md-add"/>
      安装插件
    </Button>
    <div>
      <Table border :columns="table.columns" :data="table.rows" :loading="loading"></Table>
      <Page :total="table.total" :current="table.pageNumber" :page-size="table.pageSize" show-sizer
            show-elevator show-total style="padding: 10px 0 20px 20px"
            @on-change="handleCurrentChange" @on-page-size-change='handleSizeChange'/>
    </div>
  </div>
</template>

<script>

  import BreadCrumb from "../../components/bread-crumb"
  import {PrivatePyQtRequest} from '../../js/resources'

  export default {
    name: "pluginManage",
    components: {
      BreadCrumb
    },
    data() {
      return {
        loading: false,
        queryParams: {
          searchCode: "",
        },
        table: {
          columns: [
            {title: '分类', key: 'class'},
            {title: '插件名称', key: 'name'},
            {title: '插件描述', key: 'description'},
            {title: '组织', key: 'groupId'},
            {title: '插件', key: 'artifactId'},
            {title: '版本号', key: 'version'},
            {title: '安装时间', key: 'installTime'},
            {
              title: '操作', key: 'action', fixed: 'right', align: 'center', width: 120,
              render: (h, params) => {
                return h('div', [
                  h('a', {
                    props: {
                      type: 'text',
                      size: 'small'
                    },
                    style: {
                      color: '#2D8CF0'
                    },
                    on: {
                      click: () => {
                        this.$Modal.confirm({
                          title: '确认删除',
                          content: '<p>您确认删除该记录吗？</p>',
                          okText: '删除',
                          cancelText: '取消',
                          onOk: () => {
                            this.deleteSpPlugin(params.row.groupId, params.row.artifactId)
                          }
                        })
                      }
                    }
                  }, '删除')
                ])
              }
            }
          ],
          rows: [],
          total: 0,
          pageSize: 10,
          pageNumber: 1,
        },
      }
    },
    created() {
      this.query()
    },
    methods: {
      handleSizeChange(val) {
        this.query(val)
      },
      handleCurrentChange(val) {
        this.query(undefined, val)
      },
      async query(pageSize, pageNumber) {
        if (pageSize) {
          this.table.pageSize = pageSize
        }
        if (pageNumber) {
          this.table.pageNumber = pageNumber
        }
        this.queryParams.pageSize = this.table.pageSize
        this.queryParams.pageNumber = this.table.pageNumber
        this.loading = true
        let res = await PrivatePyQtRequest({
          path: "/plugin/list",
          params: this.queryParams
        })
        this.table.rows = res
        this.loading = false
      },
      async deleteSpPlugin(groupId, artifactId) {
        let res = await PrivatePyQtRequest({
          path: "/plugin/del",
          params: {
            groupId,
            artifactId
          }
        })
        this.query()
        console.log("插件删除结果" + res)
      },
      async addSpPlugin() {
        let res = await PrivatePyQtRequest({
          path: "/plugin/open_add",
        })
        console.log("插件窗口打开结果" + res)
      },
    }
  }
</script>

<style scoped>

</style>
